package uscdb.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import uscdb.dao.AreaDaoI;
import uscdb.model.Tarea;
import uscdb.pageModel.DataGrid;
import uscdb.pageModel.Json;
import uscdb.pageModel.Larea;
import uscdb.pageModel.PageHelper;
import uscdb.service.AreaServiceI;
import uscdb.util.StringUtil.StringUtil;

@Service
public class AreaServiceImpl implements AreaServiceI {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AreaServiceImpl.class);

	@Autowired
	private AreaDaoI areaDao;

	@Override
	public void add(Larea larea) {
		larea.setId(UUID.randomUUID().toString());
		Tarea tarea = new Tarea();
		BeanUtils.copyProperties(larea, tarea);
		areaDao.save(tarea);
	}

	@Override
	public void edit(Larea larea) {
		Tarea tarea = areaDao.get(Tarea.class, larea.getId());
		if (null != tarea) {
			BeanUtils.copyProperties(larea, tarea, new String[] { "id" });
		}

	}

	@Override
	public Json delete(String ids) {
		Json json = new Json();
		Tarea tareaLog = new Tarea();
		try {
			for (String id : ids.split(",")) {
				Tarea tarea = areaDao.get(Tarea.class, id);
				tareaLog = tarea;
				areaDao.delete(tarea);
			}
			json.setMsg("删除成功");
			json.setSuccess(true);
		} catch (Exception e) {
			logger.error("The error area : " + tareaLog.toString());
			logger.error(e.getMessage(), e);
		}

		return json;
	}

	@Override
	public DataGrid dataGrid(Larea larea, PageHelper ph) {
		DataGrid dg = new DataGrid();
		List<Larea> lareaList = new ArrayList<>();
		List<Tarea> tareaList = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		String hql = "from Tarea t ";
		tareaList = areaDao.find(hql + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tarea tarea : tareaList) {
			Larea larea2Page = new Larea();
			BeanUtils.copyProperties(tarea, larea2Page);
			lareaList.add(larea2Page);
		}
		dg.setTotal(areaDao.count("select count(*) " + hql, params));
		dg.setRows(lareaList);
		return dg;
	}

	/**
	 * 排序函数
	 * 
	 * @param ph
	 *            分页对象
	 * @return
	 */
	private String orderHql(PageHelper ph) {
		String orderString = "";
		if (ph.getSort() != null && ph.getOrder() != null) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public List<Larea> areaCombobox(String q) {
		if (null == q || StringUtil.isEmpty(q)) {
			q = "";
		}
		Map<String, Object> params = new HashMap<>();
		params.put("name", "%%" + q.trim().toUpperCase() + "%%");
		String hql = "from Tarea t where t.name like :name order by name";
		// 迟早是个bug
		List<Tarea> tareaList = areaDao.find(hql, params, 1, 20);
		List<Larea> lareaList = new ArrayList<>();
		for (Tarea tarea : tareaList) {
			Larea larea = new Larea();
			larea.setName(tarea.getName());
			larea.setArea(tarea.getName());
			lareaList.add(larea);
		}
		return lareaList;
	}

}
